#include "semlib.h"
#include <sys/sem.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int sem_id = semget(IPC_PRIVATE, 1, 0666);
    if (sem_id == -1)
    {
        perror("semget");
        exit(EXIT_FAILURE);
    }

    if (semctl(sem_id, 0, SETVAL, 0) == -1)
    {
        perror("semctl");
        exit(EXIT_FAILURE);
    }

    pid_t pid = fork();
    if (pid == -1)
    {
        perror("fork");
        exit(EXIT_FAILURE);
    }

    if (pid == 0)
    {
        printf("Child running\n");
        sem_v(sem_id);
        exit(EXIT_SUCCESS);
    }
    else
    {
        sem_p(sem_id);
        printf("Parent running\n");
        semctl(sem_id, 0, IPC_RMID);
    }

    return 0;
}
